package cheme.ProcessEditor;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

import org.eclipse.core.runtime.Path;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IPersistableElement;
import org.w3c.dom.Document;

import cheme.ProcessModel.IXmlOperations;
import cheme.ProcessModel.ProcessModel;
import cheme.ProcessModel.XmlHelper;

public class ChemeFileInput implements IEditorInput
{
	Path path = null;
	File file = null;
	private boolean needsInitialSaveAs = false;
	
	public ChemeFileInput(ChemeFileInput input)
	{
		path = input.path;
		file = input.file;
		needsInitialSaveAs = input.needsInitialSaveAs;
	}
	
	public ChemeFileInput(String pathName)
	{
		setPath(pathName);
	}
	
	private void setNeedsInitialSaveAs(boolean needsInitialSaveAs)
	{
		this.needsInitialSaveAs = needsInitialSaveAs;
	}

	public boolean getNeedsInitialSaveAs()
	{
		return needsInitialSaveAs;
	}
	
	public Path getPath()
	{
		return path;
	}
	
	public void setPath(String pathName)
	{
		path = new Path(pathName);
	}
	
	public File getFile()
	{
		if(null == file)
		{
			file = getPath().toFile();
		}
		
		return file;
	}
	
	public String getFileXml()
	{
		StringBuilder fileContents = new StringBuilder();
		if(exists())
		{
			/*
			 * IF the file exists, we can read data from that.
			 */
			try
			{
				String line = null;
				BufferedReader input = new BufferedReader(new FileReader(getFile()));
				while((line = input.readLine()) != null)
				{
					fileContents.append(line);
				}
				
			}
			catch(FileNotFoundException e)
			{
				e.printStackTrace();
			}
			catch(IOException e)
			{
				e.printStackTrace();
				
			}
		}
		else
		{
			setNeedsInitialSaveAs(true);
		}
		
		return fileContents.toString();
	}
	
	public BufferedWriter getOutputStream()
	{
		BufferedWriter out = null;
		try {
			out = new BufferedWriter(new FileWriter(getPath().toString()));
			
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		return out;
	}

	public boolean exists()
	{
		return getFile().exists();
	}

	public String getName()
	{
		return getPath().lastSegment();
	}

	public ImageDescriptor getImageDescriptor()
	{
		return null;
	}

	public IPersistableElement getPersistable()
	{
		return null;
	}

	public String getToolTipText()
	{
		return getName();
	}

	public Object getAdapter(Class adapter)
	{
		return null;
	}
	
	public void writeToFile(Object model)
	{
		try
		{
			BufferedWriter out = getOutputStream();
			out.write(((IXmlOperations)model).toXml());
			out.close();
			
			setNeedsInitialSaveAs(false);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public Object readFromFile()
	{
		String xmlString = getFileXml();
		if(xmlString.isEmpty())
		{
			return null; 
		}
		Document xmlDoc = (Document)XmlHelper.xmlFromString(xmlString);
		return xmlDoc;
	}
	
	private Object readFromInputStream(InputStream is)
	{
		Object result = null;
		
		try
		{
			ObjectInputStream in = new ObjectInputStream(is);
			result = in.readObject(); // read();// 
			in.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		return result;
	}
}
